package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.home.dao.ExhibitionManageTimingMapper;
import com.eastfair.home.entity.ExhibitionManageTiming;
import com.eastfair.home.service.ExhibitionManageTimingService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.venueservice.consts.CommonConstants;
import com.eastfair.venueservice.entity.WorkOrderServiceItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import java.util.Collection;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import com.eastfair.home.vo.*;
import com.eastfair.home.dto.*;
/**
* <p>
    * 业务实现类
    * 主场展会时间安排
    * </p>
*
* @author linan
* @date 2022-10-22
*/
@Slf4j
@Service
//@DS("#thread.tenant")
public class ExhibitionManageTimingServiceImpl extends SuperServiceImpl<ExhibitionManageTimingMapper, ExhibitionManageTiming> implements ExhibitionManageTimingService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
    */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ExhibitionManageTiming> modelList) {
    modelList.stream().forEach(s -> {
    s.setIsDeleted(BusinessConstant.YES);
    });
    // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
    return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ExhibitionManageTiming model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    /**
    * 通用查询
    *
    * @param modelDTO DTO
    * @param ids 主键id集合
    * @return list
    */
    @Override
    public List<ExhibitionManageTiming> list(ExhibitionManageTimingDTO modelDTO, List<Long> ids) {
        return ((ExhibitionManageTimingService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids 主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
       magic = @Magic(
       key = "{"+
       "{'isEnabled', #args[0]?.isEnabled},"+
       "{'exhibitionManageId', #args[0]?.exhibitionManageId},"+
       "{'linkName', #args[0]?.linkName},"+
       "{'startDate', #args[0]?.startDate},"+
       "{'endDate', #args[0]?.endDate},"+
        "{'id', #args[1]}"+
        "}"))
    public List<ExhibitionManageTiming> listOfCache(ExhibitionManageTimingDTO modelDTO, List<Long> ids) {
       //查询
       QueryWrapper<ExhibitionManageTiming> queryWrapper = new QueryWrapper<>();
       if (modelDTO != null) {
                    queryWrapper.lambda().eq(modelDTO.getExhibitionManageId() != null,ExhibitionManageTiming::getExhibitionManageId,modelDTO.getExhibitionManageId());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getLinkName()),ExhibitionManageTiming::getLinkName,modelDTO.getLinkName());
                    queryWrapper.lambda().eq(modelDTO.getStartDate() != null,ExhibitionManageTiming::getStartDate,modelDTO.getStartDate());
                    queryWrapper.lambda().eq(modelDTO.getEndDate() != null,ExhibitionManageTiming::getEndDate,modelDTO.getEndDate());
            }
            if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(ExhibitionManageTiming::getId, ids);
            }
            queryWrapper.lambda().eq(ExhibitionManageTiming::getIsDeleted, BusinessConstant.DELETE_NO);
            List<ExhibitionManageTiming> modelList = list(queryWrapper);
            return modelList;
    }

    @Override
    public Boolean updateExhibitionTiming(ExhibitionManageTimingModifyDTO params) {
        Long exhibitionManageId = params.getExhibitionManageId();
        // 先删除历史记录
        update(new UpdateWrapper<ExhibitionManageTiming>().lambda()
                .eq(ExhibitionManageTiming::getExhibitionManageId,exhibitionManageId)
                .eq(ExhibitionManageTiming::getIsDeleted, BusinessConstant.NO)
                .set(ExhibitionManageTiming::getIsDeleted,BusinessConstant.YES));
        List<ExhibitionManageTimingDTO> exhibitionManageTimingList = params.getExhibitionManageTimingList();
        for (ExhibitionManageTimingDTO exhibitionManageTimingDTO : exhibitionManageTimingList) {
            exhibitionManageTimingDTO.setExhibitionManageId(exhibitionManageId);
            ExhibitionManageTiming exhibitionManageTiming = BeanUtil.copyProperties(exhibitionManageTimingDTO, ExhibitionManageTiming.class, CommonConstants.COMMON_IGNORE_PROPERTIES);
            save(exhibitionManageTiming);
        }
        return true;
    }

    @Override
    public List<ExhibitionManageTimingVO> queryExhibitionTiming(ExhibitionManageTimingPageQuery params) {
        List<ExhibitionManageTiming> list = list(new QueryWrapper<ExhibitionManageTiming>().lambda()
                .eq(ExhibitionManageTiming::getExhibitionManageId, params.getExhibitionManageId())
                .eq(ExhibitionManageTiming::getIsDeleted, BusinessConstant.NO));
        return ConvertUtil.convertList(list,ExhibitionManageTimingVO.class);
    }


    /**
     * do转vo
     * @param model model
     * @return modelVo
     */
    @Override
    public ExhibitionManageTimingVO doToVo(ExhibitionManageTiming model) {
        ExhibitionManageTimingVO modelVo = new ExhibitionManageTimingVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }
    /**
     * do转vo
     * @param modelList modelList
     * @return list
     */
     @Override
     public List<ExhibitionManageTimingVO> doToVos(List<ExhibitionManageTiming> modelList) {
        if (modelList == null || modelList.isEmpty()) {
        return null;
        }
        List<ExhibitionManageTimingVO> result = new ArrayList<>();
        for (ExhibitionManageTiming model : modelList) {
           result.add(doToVo(model));
        }
        return result;
    }
}
